//
//  BleKiUtils.m
//  BleKiSDK
//
//  Created by wulanzhou on 16/6/16.
//  Copyright © 2016年 wulanzhou. All rights reserved.
//

#import "BLEKiUtils.h"

#define kLogDirName     @"BLEKiSDK_logs"          //日志目录名称
#define kLogFileName    @"MyLogFile.log"    //日志文件名

@implementation BLEKiUtils


/**
 *  获取日志文件路径
 *
 *  @return
 */
+ (NSString *)getLogFilePath{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *root = [paths objectAtIndex:0];
    NSString *dirPath = [root stringByAppendingPathComponent:kLogDirName];
    NSString *filePath = [dirPath stringByAppendingPathComponent:kLogFileName];
    
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    if (![fileManager fileExistsAtPath:dirPath]) {
        
        
        BOOL success=YES;
        BOOL isDir = NO;
        BOOL existed = [fileManager fileExistsAtPath:dirPath isDirectory:&isDir];
        if (!(isDir == YES && existed == YES) )
        {
            success=[fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:nil];
        }
        
        if (!success) {
            NSLog(@"ct_logs createDirectoryWithPath failed!");
        }
    }
    return filePath;

}

/**
 *  判断是否可以写日志
 */
+ (BOOL)isWriteLogEnable{

    if ([[NSUserDefaults standardUserDefaults] objectForKey:kBLELocalShowLogKey]) {
        return [[NSUserDefaults standardUserDefaults] boolForKey:kBLELocalShowLogKey];
    }
    return NO;

}

/**
 *  二进制字符串转化为NData
 *
 *  @param string 二进制字符串
 *
 *  @return
 */
+ (NSData *)stringToByte:(NSString*)string{
    
    if (string&&[string isKindOfClass:[NSString class]]&&[string length]>0) {
        
        NSString *hexString=[[string uppercaseString] stringByReplacingOccurrencesOfString:@" " withString:@""];
        if ([hexString length]%2!=0) {
            return nil;
        }
        Byte tempbyt[1]={0};
        NSMutableData* bytes=[NSMutableData data];
        for(int i=0;i<[hexString length];i++)
        {
            unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)
            int int_ch1;
            if(hex_char1 >= '0' && hex_char1 <='9')
                int_ch1 = (hex_char1-48)*16;   //// 0 的Ascll - 48
            else if(hex_char1 >= 'A' && hex_char1 <='F')
                int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
            else
                return nil;
            i++;
            
            unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位)
            int int_ch2;
            if(hex_char2 >= '0' && hex_char2 <='9')
                int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
            else if(hex_char2 >= 'A' && hex_char2 <='F')
                int_ch2 = hex_char2-55; //// A 的Ascll - 65
            else
                return nil;
            
            tempbyt[0] = int_ch1+int_ch2;  ///将转化后的数放入Byte数组里
            [bytes appendBytes:tempbyt length:1];
        }
        return bytes;
    }

    return nil;
}

/**
 *  设置缓存值
 *
 *  @param object 缓存值
 *  @param key    缓存key
 */
+ (void)setValue:(id)object  forKey:(NSString*)key{
    if (object) {
        [[NSUserDefaults standardUserDefaults] setObject:object forKey:key];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

/**
 *  设置缓存值
 *
 *  @param boo    缓存值
 *  @param key    缓存key
 */
+ (void)setBool:(BOOL)boo  forKey:(NSString*)key{
    [[NSUserDefaults standardUserDefaults] setBool:boo forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

@end
